using System;
using System.Collections.Generic;
using System.Text;
using BizTalkCop.Rules.Bases;
using Microsoft.FxCop.Sdk;

namespace BizTalkCop.Rules.Naming.Rules
{
    /// <summary>
    /// Rule verifying map name is structured [SourceSchema]_[TargetSchema]
    /// </summary>
    public class MapNamesShouldIndicateSourceAndDestinationSchemas : ConfiguredRuleBase
    {
        /// <summary>
        /// Default constructor
        /// </summary>
        public MapNamesShouldIndicateSourceAndDestinationSchemas()  : base("MapNamesShouldIndicateSourceAndDestinationSchemas")
        {
        }

        /// <summary>
        /// Checks if the map name is correct
        /// </summary>
        /// <param name="type">Type to check</param>
        /// <returns>Problem collection</returns>
        public override ProblemCollection Check(TypeNode type)
        {
            if (IsMap(type))
            {
                int separatorStart = type.Name.Name.IndexOf(this.Separator);
                string after = type.Name.Name.Substring(separatorStart + this.Separator.Length);
                if (separatorStart < 0 || after.IndexOf(this.Separator) >= 0)
                {
                    Problems.Add(new Problem(GetNamedResolution("Format", this.Separator), type));
                }
            }
            return Problems;
        }

        private string Separator
        {
            get { return this.GetConfiguredSetting(Setting.Separator); }
        }

        #region Configured settings

        static MapNamesShouldIndicateSourceAndDestinationSchemas()
        {
            Dictionary<string, string> defaults = new Dictionary<string, string>();
            defaults.Add(Setting.Separator, Default.Separator);
            SetDefaults(typeof(MapNamesShouldIndicateSourceAndDestinationSchemas), defaults);
        }

        private struct Setting
        {
            public const string Separator = "Separator";
        }

        private struct Default
        {
            public const string Separator = "_";
        }

        #endregion
    }
}
